﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using Sirocco.Data.Entity;
using SiroccoTest.Common;

namespace SiroccoTest.Data.Entity
{
    /// <summary>
    ///This is a test class for CountryTest and is intended
    ///to contain all CountryTest Unit Tests
    ///</summary>
    [TestClass]
    public class CountryTest : BaseTest
    {
        private TestContext TestContextInstance;
        private const string Col1 = "XX";
        private const string UpdateValue = "XXX";

        #region Text Context
        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return TestContextInstance;
            }
            set
            {
                TestContextInstance = value;
            }
        }
        #endregion

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        public CountryTest() { }

        /// <summary>
        /// Test insert country to database using nhibernate 
        /// </summary>
        [TestMethod]
        public void CountryTestInsert()
        {
            using (var sess = GetSession())
            using (var tx = sess.BeginTransaction())
            {
                // Insert Country
                var country = new Country { Code = Col1, Name = Col1 };
                var generatedId = sess.Save(country); // Get the generated id. 

                // Find the Country
                var country1 = sess.Load<Country>(generatedId);
                Assert.IsNotNull(country1, "Insert - Country not saved");

                tx.Commit();
            }
        }

        /// <summary>
        /// Test update country to database using nhibernate 
        /// </summary>
        [TestMethod]
        public void CountryTestUpdate()
        {
            using (var sess = GetSession())
            using (var tx = sess.BeginTransaction())
            {
                // Insert Country
                var country = new Country { Code = Col1, Name = Col1 };
                var generatedId = sess.Save(country); // Get the generated id. 

                // Find the Country
                var countryIns = sess.Load<Country>(generatedId);
                Assert.IsNotNull(countryIns, "Insert - Country not saved");

                // Update Country Name
                countryIns.Name = UpdateValue;
                sess.Update(countryIns);

                // Find the Country
                var countryList = sess.CreateQuery("from Country as c where c.Name = ?").SetString(0, UpdateValue).List();
                Assert.IsTrue(countryList.Count > 0, "Update Test - No Country Found");

                tx.Commit();
            }
        }

        /// <summary>
        /// Test delete country from database using nhibernate 
        /// </summary>
        [TestMethod]
        public void CountryTestDelete()
        {
            using (var sess = GetSession())
            using (var tx = sess.BeginTransaction())
            {
                // Insert Country
                var country = new Country { Code = Col1, Name = Col1 };
                var generatedId = (long)sess.Save(country); // Get the generated id. 

                // Find the Country
                var country1 = sess.Load<Country>(generatedId);
                Assert.IsNotNull(country1, "Insert - Country not saved");

                // Delete Country 
                sess.Delete(country1);

                // Find the Country
                var countryList = sess.CreateQuery("from Country as c where c.Id = ?").SetInt64(0, generatedId).List();
                Assert.IsTrue(countryList.Count == 0, "Delete Test - Cannot delete country");

                tx.Commit();
            }
        }

        /// <summary>
        /// /// Test insert multiple country into the database
        /// </summary>
        [TestMethod]
        public void CountryTestInsertMultiple()
        {
            using (var sess = GetSession())
            using (var tx = sess.BeginTransaction())
            {
                // Insert Country
                var country = new Country { Code = Col1, Name = Col1 };
                sess.Save(country); // Get the generated id. 

                var country2 = new Country { Code = Col1, Name = Col1 };
                sess.Save(country2); // Get the generated id. 

                var countryList = sess.CreateQuery("from Country as c where c.Code = ?").SetString(0, Col1).List();
                Assert.IsTrue(countryList.Count == 2, "Insert count is not 2");

                tx.Commit();
            }
        }
    }
}
